Repository navigation
perf: load comments in one search, share thread downloads, speed up swipes - #351
Merged
Merged
Conversation
therealsama
force-pushed
the
perf/comment-fetch
branch
3 times, most recently
from
November 26, 2026 18:24
ed5e1c4 to
7e85b3a
Compare
therealsama
force-pushed
the
perf/comment-fetch
branch
from
November 26, 2026 19:45
7e85b3a to
5c99533
Compare
…wipes A big thread's comments took up to 15 s to load. Algolia was paged 200 hits at a time, and Firebase reads went in batches of 10, a leftover from when items were read over REST. Reads now share one WebSocket, so batches only add round trips. - One Algolia search per thread, for up to 1000 hits, its cap. It asks for the four fields a comment needs and no highlighting, which makes the response 70% smaller. An empty attributesToHighlight is ignored, so it takes an empty list. - Firebase reads go out all at once: for ordering, missing parents and reply checks. - A prefetch orders the whole thread, not just its top level, so the cache's orderedDepth and the hook's ordering completion are gone. - With no copy of the item, its comments load alongside it, from the same Firebase read. - A thread with more than 1000 comments still merges Algolia's item tree. A Firebase count over 1000 now starts it alongside the search, and it is cancelled when search turns out to have every comment. - A comment page drops a story's comments when there are more than 1000, so a count of them, under 1 KB, now cancels that search. On mobile, the swipe viewer prefetches each other feed's list and first thread so a feed switch shows at once. A story often heads two feeds, so its thread was downloaded by the feed prefetch, the next-items prefetch and the story on screen at the same time. - Callers that want the same thread share one download. An abort only detaches its caller, and the download stops once all have. - Lists go out together, not one feed after another 500 ms apart, and threads follow one at a time as their lists arrive, so a stalled list holds up nothing else. - It prefetches the story a feed opens on, skipping ones read in an earlier session, and only a thread with no copy: a stale one shows at once and refreshes when opened. - It starts once the current feed's stories are on screen, including when a stale cache shows during a stalled refresh. A 772-comment thread loads in 1.3 s (was 4.9 s), and a 3,378-comment one in under 4.2 s (was 15.1 s). On slow 4G, switching to a feed whose thread is still prefetching takes 8.7 s (was 26.1 s). Opening a story from the list, by its comments or a text post's title, cancelled every prefetch, its own too, so its page downloaded the thread again. That one now keeps running and the page joins it. On desktop, a 200-comment story opened mid-prefetch shows its comments in 468 ms (was 877 ms). Each swipe re-rendered every comment in the five mounted panels, and each render did more work than it needed to. - Comment is memoized. - A comment's timestamp tooltip reuses one Intl.DateTimeFormat, where toLocaleString built a new one per call. - index.html has a <base> with no href. React Router's hash history looks one up for every link, which scanned the whole page when there was none. - A comment is sanitized once, not twice, unless auto-linking added a link. On Pixel 7 emulation at 4x CPU, 8 swipes take 37-45% less main-thread time, and opening a 3,815-comment thread cuts its longest task from 525 ms to 425 ms.
therealsama
force-pushed
the
perf/comment-fetch
branch
from
November 26, 2026 21:59
5c99533 to
8292910
Compare
therealsama
enabled auto-merge
November 26, 2026 22:46
kplachkov
approved these changes
Nov 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Big threads took up to 15 s to load their comments, the same thread was often downloaded several times at once, and each swipe re-rendered every comment in the mounted panels.
Changes
Loading comments
Sharing downloads
Swiping
Commentis memoized, its tooltip reuses oneIntl.DateTimeFormat, and it is sanitized once unless auto-linking added a link.index.htmlhas a<base target="_self">, so React Router'squerySelector("base")on every link no longer scans the whole page.Results
Measured against live HN:
Trade-offs
Testing
npm run typecheck: passednpm run lint: passednpm run test:run: passed, 1224 testsnpx playwright test --reporter=line --retries=0: 919 passed, 44 skipped, 1 failed (Mobile Safaritouch-feel.spec.ts:33, a flaky swipe test that passed 3 of 3 when rerun alone). CI passed.